﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using BrainTechLLC;
using BrainTechLLC.WPF;
using System.Windows.Data;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows;
using System.Collections.Specialized;
using DirectoryViewModels;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC.FolderExplorer
{
    public partial class FolderViewModel : BaseNotifyPropertyChangedUI
    {
        protected void DoFolderExpansion()
        {
            var folders = GetAllFoldersInMemory();

            List<FolderViewModel> toExpand = new List<FolderViewModel>();
            List<FolderViewModel> toCollapse = new List<FolderViewModel>();
            for (int n = 0; n < folders.Count; n++)
            {
                var folder = folders[n];
                if (folder.Depth <= _FolderExpansionLevel)
                {
                    if (!folder._isExpanded)
                    {
                        toExpand.Add(folder);
                    }
                }
                else
                {
                    if (folder._isExpanded)
                    {
                        toCollapse.Add(folder);
                    }
                }
            }

            bool done1 = toExpand.Count == 0;
            bool done2 = toCollapse.Count == 0;

            for (int n = 0; n < toExpand.Count; n++)
            {
                Application.Current.Dispatcher.BeginInvoke(new OneParamDelegate((o) =>
                    {
                        toExpand[(int)o].IsExpanded = true;
                        if ((int)o == toExpand.Count - 1)
                            done1 = true;
                    }), DispatcherPriority.Loaded, n);
            }

            for (int n = 0; n < toCollapse.Count; n++)
            {
                Application.Current.Dispatcher.BeginInvoke(new OneParamDelegate((o) =>
                    {
                        toCollapse[(int)o].IsExpanded = false;
                        if ((int)o == toCollapse.Count - 1)
                            done2 = true;
                    }), DispatcherPriority.Loaded, n);
            }

            while (!(done1 && done2))
            {
                Thread.Sleep(100);
            }

            FolderViewModel f = FindRoot();
            f.RemoveOperation("FolderExpansionLevel");
        }

        public void ExpandAllChildrenAsync()
        {
            ThreadStart ts = new ThreadStart(ExpandAllChildrenWorker);
            Thread t = new Thread(ts) { IsBackground = true, Priority = ThreadPriority.BelowNormal };
            t.Name = "ExpandAllChildrenWorker";
            t.Start();
        }

        private void ExpandAllChildrenWorker()
        {
            Application.Current.Dispatcher.BeginInvoke(new VoidDelegate(() => IsExpanded = true));

            List<FolderViewModel> subFolders = null;

            if (SubFoldersCached == null)
            {
                Application.Current.Dispatcher.BeginInvoke(new VoidDelegate(() => subFolders = SubFolders.ToList()));
            }
            else
            {
                subFolders = SubFoldersCached.ToList();
            }

            while (subFolders == null)
            {
                Thread.Sleep(100);
            }

            for (int n = 0; n < subFolders.Count; n++)
            {
                var folder = subFolders[n];
                folder.ExpandAllChildren();
            }
        }

        public void ExpandAllChildren()
        {
            Application.Current.Dispatcher.BeginInvoke(new VoidDelegate(() => IsExpanded = true), DispatcherPriority.Render);

            for (int n = 0; n < SubFolders.Count; n++)
            {
                var folder = SubFolders[n];
                folder.ExpandAllChildren();
            }
        }
    }
}
